/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2022. All rights reserved.
 */

import { isWindows } from '@ohos/hvigor';

import { HvigorConfigConst } from '../../const/common-const';
import { ModuleModelImpl } from '../../model/module/module-model-impl';
import { FileUtil } from '../../utils/file-util.js';
import { JavaCommandBuilder } from '../java-command-builder.js';

export class PackingToolOptions extends JavaCommandBuilder {
  constructor() {
    super();
    this.setJavaSystemProperty('file.encoding', isWindows() ? 'GBK' : 'utf-8');
  }

  addFieldAndPath(field: string, path: string) {
    FileUtil.checkPathLength(path);
    this.commandList.push(field);
    this.commandList.push(path);
    return this;
  }

  addMode(mode: string): PackingToolOptions {
    this.commandList.push('--mode');
    this.commandList.push(mode);
    return this;
  }

  addHapPath(hapPath: string): PackingToolOptions {
    hapPath.split(',').forEach(FileUtil.checkPathLength);
    this.commandList.push('--hap-path');
    this.commandList.push(hapPath);
    return this;
  }

  addHspPath(hspPath: string): PackingToolOptions {
    hspPath.split(',').forEach(FileUtil.checkPathLength);
    this.commandList.push('--hsp-path');
    this.commandList.push(hspPath);
    return this;
  }

  addHarPath(harPath: string): PackingToolOptions {
    return this.addFieldAndPath('--har-path', harPath);
  }

  addPackInfoPath(packInfoPath: string): PackingToolOptions {
    return this.addFieldAndPath('--pack-info-path', packInfoPath);
  }

  addPkgContextInfoPath(pkgContextInfoPath: string): PackingToolOptions {
    return this.addFieldAndPath('--pkg-context-path', pkgContextInfoPath);
  }

  /**
   * 1.hsp包文件路径，文件名必须以.hsp为后缀。如果是多个hsp包需要英文","分隔。2.hsp包目录。
   *
   * @param hspList
   */
  addHspList(hspList: string) {
    return this.addFieldAndPath('--hsp-list', hspList);
  }

  /**
   * 指定的版本号，hsp的版本号会被修改为该版本。需要为整数
   *
   * @param versionCode
   */
  addVersionCode(versionCode: string) {
    return this.addFieldAndPath('--version-code', versionCode);
  }

  /**
   * 指定bundleName
   *
   * @param bundleName
   */
  addBundleName(bundleName: string) {
    return this.addFieldAndPath('--bundle-name', bundleName);
  }

  /**
   * 传入一个路径数组, 传递给打包工具需要打包到hap的目录的路径.
   *
   * 打包工具doc:
   * 在命令行中新增一个 --dir-list 命令，支持将输入的目录打入hap包
   * 使用命令如：
   * java -jar \app_packing_tool.jar --mode hap
   * --dir-list \dir1,\dir2,\dir3
   * --out-path \stage.hap
   * --force true
   *
   * @param {string[]} dirList 需要打到hap包里所有的目录
   * @returns {PackingToolOptions}
   */
  addDirList(dirList: string[]): PackingToolOptions {
    for (const dirPath of dirList) {
      FileUtil.checkPathLength(dirPath);
    }
    this.commandList.push('--dir-list');
    this.commandList.push(dirList.join(','));
    return this;
  }

  addOutPath(outPath: string): PackingToolOptions {
    return this.addFieldAndPath('--out-path', outPath);
  }

  addFilePath(filePath: string): PackingToolOptions {
    return this.addFieldAndPath('--file-path', filePath);
  }

  addProjectPath(projectPath: string): PackingToolOptions {
    return this.addFieldAndPath('--project-path', projectPath);
  }

  addBinPath(binPath: string): PackingToolOptions {
    return this.addFieldAndPath('--bin-path', binPath);
  }

  addJsonPath(jsonPath: string): PackingToolOptions {
    return this.addFieldAndPath('--json-path', jsonPath);
  }

  addPackResPath(packResPath: string): PackingToolOptions {
    return this.addFieldAndPath('--pack-res-path', packResPath);
  }

  addEntryCardPath(entryCardFile: string): PackingToolOptions {
    return this.addFieldAndPath('--entrycard-path', entryCardFile);
  }

  addResourcesPath(resourcesPath: string): PackingToolOptions {
    return this.addFieldAndPath('--resources-path', resourcesPath);
  }

  addResourceTableTxtPath(txtPath: string): PackingToolOptions {
    return this.addFieldAndPath('--txt-path', txtPath);
  }

  addJsPath(jsPath: string): PackingToolOptions {
    return this.addFieldAndPath('--js-path', jsPath);
  }

  addEtsPath(etsPath: string): PackingToolOptions {
    return this.addFieldAndPath('--ets-path', etsPath);
  }

  addAssetsPath(assetPath: string): PackingToolOptions {
    return this.addFieldAndPath('--assets-path', assetPath);
  }

  addIndexPath(indexPath: string): PackingToolOptions {
    return this.addFieldAndPath('--index-path', indexPath);
  }

  addLibPath(libPath: string): PackingToolOptions {
    return this.addFieldAndPath('--lib-path', libPath);
  }

  addJarPath(jarPath: string): PackingToolOptions {
    return this.addFieldAndPath('--jar-path', jarPath);
  }

  addAnPath(anPath: string): PackingToolOptions {
    return this.addFieldAndPath('--an-path', anPath);
  }

  addSysCapPath(sysCapPath: string): PackingToolOptions {
    return this.addFieldAndPath('--rpcid-path', sysCapPath);
  }

  addMainModuleLimit(mainPackageLimitSize: number): PackingToolOptions {
    this.commandList.push('--main-module-limit');
    this.commandList.push(mainPackageLimitSize.toString());
    return this;
  }

  addNormalModuleLimit(normalPackageLimitSize: number): PackingToolOptions {
    this.commandList.push('--normal-module-limit');
    this.commandList.push(normalPackageLimitSize.toString());
    return this;
  }

  addTotalLimit(totalPackageLimitSize: number): PackingToolOptions {
    this.commandList.push('--total-limit');
    this.commandList.push(totalPackageLimitSize.toString());
    return this;
  }

  addCompressLevel(level: string, apiVersion: number) {
    const specificLevel = HvigorConfigConst.OHOS_PACK_COMPRESS_LEVEL_MAP.get(level);
    if (apiVersion && apiVersion < 12) {
      return this;
    }
    if (specificLevel) {
      this.commandList.push('--compress-level');
      this.commandList.push(specificLevel);
    }
    return this;
  }

  addPacArg(pacFilePath: string) {
    this.commandList.push('--pac-json-path');
    this.commandList.push(pacFilePath);
    return this;
  }

  force(force: boolean): PackingToolOptions {
    this.commandList.push('--force');
    this.commandList.push(force.toString());
    return this;
  }
}
